package com.syni.mdd.sjb.common.datatreaure.controller.protect.minapp;

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckUserIdentity;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiCoupon.SearchCouponDetailDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiCoupon.SearchDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiCoupon.SearchTodayYesDayVerCouponDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiCoupon.SearchVerCouponDetailDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.CouponRestService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultCode;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.controller.BaseController;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.minAppLogin.SearchConsumerCouponDetailRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.minAppLogin.SearchConsumerCouponRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.minAppLogin.SearchTodayYesDayVerCouponRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.minAppLogin.VerConsumerCouponDetailRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.saasminapp.PushTemplateRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.BindGroupRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.GetNewsIdRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.SaveGroupInfoVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.SearchConsumerCouponVo;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppMinappInfoService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppNewsService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserChannelService;
import com.syni.mdd.sjb.common.datatreaure.utils.TokenUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/minApp/ver")
public class BusinessVerController extends BaseController {

    @Autowired
    TokenUtils tokenUtils;

    @Autowired
    UserRestService userRestService;

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    @Autowired
    BusinessRestService businessRestService;

    @Autowired
    CouponRestService couponRestService;
    @Autowired
    DxshAppMinappInfoService dxshAppMinappInfoService;
    @Autowired
    DxshAppNewsService dxshAppNewsService;

    public static Map<String, PushTemplateRpVo> pushTemplateMap = new ConcurrentHashMap<>();


    @PostMapping("/searchConsumerCoupon")
    @ApiOperation(value = "查询消费券内容")
    @CheckUserIdentity
    public Map<String,Object> searchConsumerCoupon(@RequestBody SearchConsumerCouponVo searchConsumerCouponVo){
        if(searchConsumerCouponVo.getSearchWay() == Constants.SEARCH_WAY_BY_NEW_WAY){
            if(searchConsumerCouponVo.getSearchBody().indexOf("Syni_MDD") != -1){
                String sArray[] = searchConsumerCouponVo.getSearchBody().split("_");
                String verData = sArray[2];
                String verNo = verData.split("=")[1];
                searchConsumerCouponVo.setSearchWay(Constants.SEARCH_WAY_BY_CODE);
                searchConsumerCouponVo.setCode(verNo);
            }
        }
        SearchDto searchDto = new SearchDto();
        BeanUtils.copyProperties(searchConsumerCouponVo,searchDto);
        SearchConsumerCouponRpVo searchConsumerCouponRpVo = new SearchConsumerCouponRpVo();
        RpcResponseVo rpcResponseVo = couponRestService.search(searchDto);
        if(!rpcResponseVo.isSuccess()){
            if(rpcResponseVo.getStatus().equals(ResultCode.CODE_ERROR_05)){
                return ResultMapHelper.success(searchConsumerCouponRpVo, ResultMsg.MSG_GET_SUCCESS);
            }
        }
        searchDto = rpcResponseVo.parseObject(SearchDto.class);
        searchConsumerCouponRpVo = BeanUtils.copyProperties(searchDto,SearchConsumerCouponRpVo.class);
        if(searchDto.getBmsGroupBuy() != null) {
            searchConsumerCouponRpVo.setBmsGroupBuy(BeanUtils.copyProperties(searchDto.getBmsGroupBuy(), SearchConsumerCouponRpVo.BmsGroupBuyData.class));
        }
        if(searchDto.getUserReceCoupon() != null){
            searchConsumerCouponRpVo.setUserReceCoupon(BeanUtils.copyProperties(searchDto.getUserReceCoupon(), SearchConsumerCouponRpVo.UserReceCoupon.class));
        }
        return ResultMapHelper.success(searchConsumerCouponRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @PostMapping("/searchConsumerCouponDetail")
    @ApiOperation(value = "查询消费券内容详情")
    @CheckUserIdentity
    public Map<String,Object> searchConsumerCouponDetail(@RequestBody SearchConsumerCouponVo searchConsumerCouponVo){
        SearchCouponDetailDto searchCouponDetailDto = new SearchCouponDetailDto();
        searchCouponDetailDto.setVerId(searchConsumerCouponVo.getVerId());
        RpcResponseVo rpcResponseVo = couponRestService.searchCouponDetail(searchCouponDetailDto);
        if(!rpcResponseVo.isSuccess()){
            if(rpcResponseVo.getStatus().equals(ResultCode.CODE_ERROR_05)){
                return ResultMapHelper.result(ResultCode.CODE_ERROR_05,ResultMsg.CODE_ERROR_05);
            }
        }
        searchCouponDetailDto = rpcResponseVo.parseObject(SearchCouponDetailDto.class);
        SearchConsumerCouponDetailRpVo searchConsumerCouponDetailRpVo = BeanUtils.copyProperties(searchCouponDetailDto,SearchConsumerCouponDetailRpVo.class);
        if(searchCouponDetailDto.getBuyDetailData() != null) {
            searchConsumerCouponDetailRpVo.setBuyDetailData(BeanUtils.copyProperties(searchCouponDetailDto.getBuyDetailData(), SearchConsumerCouponDetailRpVo.BmsGroupBuyDetailData.class));
            searchConsumerCouponDetailRpVo.getBuyDetailData().setBusiness(BeanUtils.copyProperties(searchCouponDetailDto.getBuyDetailData().getBusinessData(), SearchConsumerCouponDetailRpVo.BusinessData.class));
        }
        if(searchCouponDetailDto.getUserReceDetailData() != null){
            searchConsumerCouponDetailRpVo.setUserReceDetailData(BeanUtils.copyProperties(searchCouponDetailDto.getUserReceDetailData(), SearchConsumerCouponDetailRpVo.UserReceDetailData.class));
            searchConsumerCouponDetailRpVo.getUserReceDetailData().setBusiness(BeanUtils.copyProperties(searchCouponDetailDto.getUserReceDetailData().getBusiness(), SearchConsumerCouponDetailRpVo.BusinessData.class));
            if(StringUtils.isBlank(searchConsumerCouponDetailRpVo.getUserReceDetailData().getCouponDesc())){
                searchConsumerCouponDetailRpVo.getUserReceDetailData().setCouponDesc("");
            }
        }
        return ResultMapHelper.success(searchConsumerCouponDetailRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @PostMapping("/searchTodayYesDayVerCoupon")
    @ApiOperation(value = "查询今天和昨天的核销详情")
    @CheckUserIdentity
    public Map<String,Object> searchTodayYesDayVerCoupon(@RequestBody SearchConsumerCouponVo searchConsumerCouponVo){
        SearchTodayYesDayVerCouponDto searchTodayYesDayVerCouponDto = new SearchTodayYesDayVerCouponDto();
        searchTodayYesDayVerCouponDto.setUserId(searchConsumerCouponVo.getUserId());
        searchTodayYesDayVerCouponDto.setBusinessId(searchConsumerCouponVo.getBusinessId());
        RpcResponseVo rpcResponseVo = couponRestService.searchTodayYesDayVerCoupon(searchTodayYesDayVerCouponDto);
        if(!rpcResponseVo.isSuccess()){
            if(rpcResponseVo.getStatus().equals(ResultCode.CODE_ERROR_05)){
                return ResultMapHelper.result(ResultCode.CODE_ERROR_05,ResultMsg.CODE_ERROR_05);
            }
            return ResultMapHelper.result(rpcResponseVo.getStatus(),rpcResponseVo.getMsg());
        }
        searchTodayYesDayVerCouponDto = rpcResponseVo.parseObject(SearchTodayYesDayVerCouponDto.class);
        List<SearchTodayYesDayVerCouponDto.VerRecord> verRecordRps = searchTodayYesDayVerCouponDto.getVerRecordRps();
        List<SearchTodayYesDayVerCouponRpVo> searchTodayYesDayVerCouponRpVo = BeanUtils.copyList(verRecordRps, SearchTodayYesDayVerCouponRpVo.class);

        return ResultMapHelper.success(searchTodayYesDayVerCouponRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @PostMapping("/verConsumerCouponDetail")
    @ApiOperation(value = "查询消费券核销详情")
    @CheckUserIdentity
    public Map<String,Object> verConsumerCouponDetail(@RequestBody SearchConsumerCouponVo searchConsumerCouponVo){
        SearchVerCouponDetailDto searchVerCouponDetailDto = new SearchVerCouponDetailDto();
        searchVerCouponDetailDto.setVerRecordId(searchConsumerCouponVo.getVerRecordId());
        searchVerCouponDetailDto.setBusinessId(searchConsumerCouponVo.getBusinessId());
        RpcResponseVo rpcResponseVo = couponRestService.searchVerCouponDetail(searchVerCouponDetailDto);
        if(!rpcResponseVo.isSuccess()){
            if(rpcResponseVo.getStatus().equals(ResultCode.CODE_ERROR_05)){
                return ResultMapHelper.result(ResultCode.CODE_ERROR_05,ResultMsg.CODE_ERROR_05);
            }
        }
        searchVerCouponDetailDto = rpcResponseVo.parseObject(SearchVerCouponDetailDto.class);
        System.out.println(searchVerCouponDetailDto);
        VerConsumerCouponDetailRpVo verConsumerCouponDetailRpVo = BeanUtils.copyProperties(searchVerCouponDetailDto,VerConsumerCouponDetailRpVo.class);
        if(searchVerCouponDetailDto.getBuyDetailData() != null) {
            verConsumerCouponDetailRpVo.setBuyDetailData(BeanUtils.copyProperties(searchVerCouponDetailDto.getBuyDetailData(), VerConsumerCouponDetailRpVo.BmsGroupBuyDetailData.class));
            verConsumerCouponDetailRpVo.getBuyDetailData().setBusiness(BeanUtils.copyProperties(searchVerCouponDetailDto.getBuyDetailData().getBusiness(), VerConsumerCouponDetailRpVo.BusinessData.class));
        }
        if(searchVerCouponDetailDto.getBmsDxOrders() != null){
            verConsumerCouponDetailRpVo.setOrder(BeanUtils.copyProperties(searchVerCouponDetailDto.getBmsDxOrders(),VerConsumerCouponDetailRpVo.BmsDxOrderData.class));
        }
        if(searchVerCouponDetailDto.getVerData() != null){
            verConsumerCouponDetailRpVo.setVerData(BeanUtils.copyProperties(searchVerCouponDetailDto.getVerData(),VerConsumerCouponDetailRpVo.VerRecordData.class));
        }
        if(searchVerCouponDetailDto.getUserReceDetailData() != null){
            verConsumerCouponDetailRpVo.setUserReceDetailData(BeanUtils.copyProperties(searchVerCouponDetailDto.getUserReceDetailData(), VerConsumerCouponDetailRpVo.UserReceDetailData.class));
            verConsumerCouponDetailRpVo.getUserReceDetailData().setBusiness(BeanUtils.copyProperties(searchVerCouponDetailDto.getUserReceDetailData().getBusiness(), VerConsumerCouponDetailRpVo.BusinessData.class));
            if(StringUtils.isBlank(verConsumerCouponDetailRpVo.getUserReceDetailData().getCouponDesc())){
                verConsumerCouponDetailRpVo.getUserReceDetailData().setCouponDesc("");
            }
        }
        return ResultMapHelper.success(verConsumerCouponDetailRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @PostMapping("/acceptGroupNews")
    @ApiOperation(value = "接收关联APPID成功通知")
    public Map<String,Object> acceptGroupNews(@RequestBody SaveGroupInfoVo saveGroupInfoVo){
        return dxshAppMinappInfoService.saveMinappInfo(saveGroupInfoVo);
    }

    @PostMapping("/cancelRelation")
    @ApiOperation(value = "接收取消关联申请通知")
    public Map<String,Object> cancelRelation(@RequestBody SaveGroupInfoVo saveGroupInfoVo){
        return dxshAppMinappInfoService.cancelRelation(saveGroupInfoVo);
    }

    @PostMapping("/cancelOperate")
    @ApiOperation(value = "取消关联弹窗内部操作（拒绝/确定）")
    @CheckUserIdentity
    public Map<String,Object> cancelOperate(@RequestBody SaveGroupInfoVo saveGroupInfoVo){
        return dxshAppMinappInfoService.cancelOperate(saveGroupInfoVo);
    }

    @PostMapping("/quitBusinessGroup")
    @ApiOperation(value = "商户退出商圈")
    @CheckUserIdentity
    public Map<String,Object> quitBusinessGroup(@RequestBody SaveGroupInfoVo saveGroupInfoVo){
        return dxshAppMinappInfoService.quitBusinessGroup(saveGroupInfoVo);
    }


    @PostMapping("/cancelOrConfirmNews")
    @ApiOperation(value = "接收取消关联操作通知")
    public Map<String,Object> cancelOrConfirmNews(@RequestBody SaveGroupInfoVo saveGroupInfoVo){
        return dxshAppMinappInfoService.cancelOrConfirmNews(saveGroupInfoVo);
    }

    @PostMapping("/clickQuitBtn")
    @ApiOperation(value = "修改信息未已读（点击我知道啦）")
    @CheckUserIdentity
    public Map<String,Object> clickQuitBtn(@RequestBody GetNewsIdRqVo getNewsIdRqVo){
        return dxshAppMinappInfoService.clickQuitBtn(getNewsIdRqVo.getNewsId());
    }

    @PostMapping("/dividedNews")
    @ApiOperation(value = "接收设置分账通知")
    public Map<String,Object> dividedNews(@RequestBody SaveGroupInfoVo saveGroupInfoVo){
        return dxshAppMinappInfoService.dividedNews(saveGroupInfoVo);
    }

    @PostMapping("/confirmDivided")
    @ApiOperation(value = "商户确认分账关系")
    @CheckUserIdentity
    public Map<String,Object> confirmDivided(@RequestBody SaveGroupInfoVo saveGroupInfoVo){
        return dxshAppMinappInfoService.confirmDivided(saveGroupInfoVo);
    }


    @PostMapping("/getGroupInfo")
    @ApiOperation(value = "获取关联商圈信息")
    @CheckUserIdentity
    public Map<String,Object> getGroupInfo(@RequestBody SaveGroupInfoVo saveGroupInfoVo){
        return dxshAppMinappInfoService.getGroupInfo(saveGroupInfoVo);
    }

    @PostMapping("/bindGroup")
    @ApiOperation(value = "关联商圈")
    @CheckUserIdentity
    public Map<String,Object> bindGroup(@RequestBody BindGroupRqVo bindGroupRqVo){
        return dxshAppMinappInfoService.bindGroup(bindGroupRqVo);
    }
}
